#pragma once

#include "JCBaseConvertor.h"

#include <vector>

namespace JCC
{
	template<class T>
	class CJCC
	{
	public:
		CJCC(void);
		~CJCC(void);

		static std::string ToJson(T* object_);

		static std::string ToJson(std::vector<T>* mulit_object_);

		static bool ToObject(T* object_, std::string json_);

		static bool ToObject(std::vector<T>* mulit_object_, std::string json_);
	};

	template<class T>
	bool JCC::CJCC<T>::ToObject( std::vector<T>* mulit_object_, std::string json_ )
	{
		bool _isnewonejson = false;
		std::string _onejson;

		for(int i=0; i<json_.length(); i++)
		{
			if(json_[i] == '{')
			{
				_isnewonejson = true;

				_onejson.swap(std::string());
			}

			if(_isnewonejson)
			{
				char cc[2] = {0};

				cc[0] = json_[i];

				_onejson.append(cc);
			}

			if(json_[i] == '}')
			{
				_isnewonejson = false;

 				T _childcls;

				CJCBaseConvertor* _convertor = &_childcls;

				if(!_convertor->ToObject(_onejson))
				{
					return false;
				}

				mulit_object_->push_back(_childcls);
			}
		}

		return true;
	}

	template<class T>
	bool JCC::CJCC<T>::ToObject( T* object_, std::string json_ )
	{
		CJCBaseConvertor* _convertor = object_;

		return _convertor->ToObject(json_);
	}

	template<class T>
	std::string JCC::CJCC<T>::ToJson( std::vector<T>* mulit_object_ )
	{
		std::string ret;

		ret.append("[");

		std::vector<T>::iterator _beginitr = mulit_object_->begin();

		for(int i=0; _beginitr!=mulit_object_->end(); _beginitr++, i++)
		{
			CJCBaseConvertor* _convertor = &((*mulit_object_)[i]);

			ret.append(_convertor->ToJson());
			ret.append(",");
		}

		if(ret[ret.length() - 1] == ',')
		{
			ret.erase(ret.length() - 1, 1);
		}

		ret.append("]");

		return ret;
	}

	template<class T>
	std::string JCC::CJCC<T>::ToJson( T* object_ )
	{
		CJCBaseConvertor* _convertor = object_;

		return _convertor->ToJson();
	}

	template<class T>
	JCC::CJCC<T>::~CJCC( void )
	{

	}

	template<class T>
	JCC::CJCC<T>::CJCC( void )
	{

	}
}
